package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.dto.CommentQueryDto;
import com.example.pojo.entity.Comment;
import com.example.pojo.vo.CommentQueryVo;
import com.example.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 评论 controller 层
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/send")
    public Result sendComment(@RequestBody Comment comment) {
        this.commentService.sendComment(comment);
        return Result.success();
    }

    @PostMapping("/reply")
    public Result replyComment(@RequestBody Comment comment) {
        this.commentService.replyComment(comment);
        return Result.success();
    }

    @PostMapping("/pageQuery/person")
    public Result pageQueryPersonComment(@RequestBody CommentQueryDto dto) {
        PageResult<CommentQueryVo> pageResult = this.commentService.pageQueryPersonComment(dto);
        return Result.success(pageResult);
    }

    @GetMapping("/query/tree/")
    public Result queryCommentTree(@RequestParam Integer postId) {
        List<CommentQueryVo> list = this.commentService.queryCommentTree(postId);
        return Result.success(list);
    }

    @GetMapping("/total")
    public Result queryPostTotalComment(@RequestParam Integer postId) {
        long total = this.commentService.queryPostTotalComment(postId);
        return Result.success(total);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteCommentAndChildComment(@PathVariable Integer id) {
        this.commentService.deleteCommentAndChildComment(id);
        return Result.success();
    }
}
